package com.turnengine.client.global.user.command;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Login User.
 */
public class LoginUser implements ILoginUser {

	/** The command id. */
	public static final short COMMAND_ID = 504;

	/** The name or email. */
	private String nameOrEmail;
	/** The password. */
	private String password;
	/** The address. */
	private int address;

	/**
	 * Creates a new Login User.
	 */
	public LoginUser() {
	}

	/**
	 * Creates a new Login User.
	 * @param nameOrEmail the name or email
	 * @param password the password
	 * @param address the address
	 */
	public LoginUser(String nameOrEmail, String password, int address) {
		setNameOrEmail(nameOrEmail);
		setPassword(password);
		setAddress(address);
	}

	/**
	 * Creates a new Login User.
	 * @param iLoginUser the i login user
	 */
	public LoginUser(ILoginUser iLoginUser) {
		setNameOrEmail(iLoginUser.getNameOrEmail());
		setPassword(iLoginUser.getPassword());
		setAddress(iLoginUser.getAddress());
	}

	/**
	 * Returns the name or email.
	 * @return the name or email.
	 */
	public String getNameOrEmail() {
		return nameOrEmail;
	}

	/**
	 * Returns the password.
	 * @return the password.
	 */
	public String getPassword() {
		return password;
	}

	/**
	 * Returns the address.
	 * @return the address.
	 */
	public int getAddress() {
		return address;
	}

	/**
	 * Sets the name or email.
	 * @param nameOrEmail the name or email to set.
	 */
	public void setNameOrEmail(String nameOrEmail) {
		if (nameOrEmail == null) {
			throw new NullPointerException("nameOrEmail");
		}
		this.nameOrEmail = nameOrEmail;
	}

	/**
	 * Sets the password.
	 * @param password the password to set.
	 */
	public void setPassword(String password) {
		if (password == null) {
			throw new NullPointerException("password");
		}
		this.password = password;
	}

	/**
	 * Sets the address.
	 * @param address the address to set.
	 */
	public void setAddress(int address) {
		this.address = address;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(251, 37);
		hash.append(nameOrEmail);
		hash.append(password);
		hash.append(address);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof LoginUser) {
			LoginUser compare = (LoginUser) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.nameOrEmail, compare.nameOrEmail);
			equals.append(this.password, compare.password);
			equals.append(this.address, compare.address);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(ILoginUser compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.nameOrEmail, compare.getNameOrEmail());
		builder.append(this.password, compare.getPassword());
		builder.append(this.address, compare.getAddress());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		if (nameOrEmail != null) {
			string.append("nameOrEmail", nameOrEmail);
		}
		if (password != null) {
			string.append("password", password);
		}
		string.append("address", address);
		return string.toString();
	}

	public short getCommandId() {
		return COMMAND_ID;
	}
}
